package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.entity.SysUser;
import com.chhuang.ewcrm.service.SysUserService;
import com.chhuang.ewcrm.string.Md5Util;
import com.chhuang.ewcrm.vo.ResultBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * @author chhuang
 * @create 2020-10-23-11:11
 */
@RestController
@RequestMapping("wapi/login")
public class WLoginController {
    Logger logger= LoggerFactory.getLogger(SysUserController.class);

    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;


    @PostMapping()
    public ResultBean<SysUser> login(String username, String password, HttpServletRequest request){
        ResultBean<SysUser> resultBean = new ResultBean<>();
        resultBean.setSuccess(true);
        if(StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)){
            SysUser user = new SysUser();
            user.setUsername(username);
            List<SysUser> users = sysUserService.query(user);
            if(users!=null && !users.isEmpty()){//用户名登录
                user = users.get(0);
            }else {
                user.setUsername(null);
                user.setUserphone(username);
                users = sysUserService.query(user);
                if(users!=null && !users.isEmpty()){//手机号登录
                    user = users.get(0);
                }else {
                    user.setUserphone(null);
                    user.setEmail(username);
                    users = sysUserService.query(user);
                    if(users!=null && !users.isEmpty()){//邮箱号登录
                        user = users.get(0);
                    }else {
                        resultBean.setSuccess(false);
                        resultBean.setMessage("用户名不存在");
                    }
                }
            }
            if(resultBean.isSuccess()){
                if(Md5Util.md5(password, Md5Util.md5(user.getUsername())).equals(user.getPassword())){
                    String token = UUID.randomUUID().toString().replaceAll("-","");
                    user.setWsLoginToken(token);
                    user.setLastLoginDevice("PC");
                    user.setLastLoginIp(request.getRemoteAddr());
                    Date now = new Date();
                    user.setWsLastLoginTime(now);
                    user.setModifyTime(now);
                    resultBean.setData(sysUserService.update(user));
                }else {
                    resultBean.setSuccess(false);
                    resultBean.setMessage("密码错误");
                }
            }
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 获取过磅程序用户的登录名
     * @return
     */
    @GetMapping("getWeighMans")
    public ResultBean<List<String>> getWeighMans(){
        ResultBean<List<String>> resultBean = new ResultBean<>();
        List<String> names = new LinkedList<>();
        SysUser sysUser = new SysUser();

        String[] roleIds = {"005","001"};
        for (String id : roleIds){
            sysUser.setRoleId(id);
            List<SysUser> users = sysUserService.query(sysUser, "ws_last_login_time desc");
            if(users!=null && !users.isEmpty()){
                for (SysUser u: users) {
                    names.add(u.getUsername());
                }
            }
        }

        resultBean.setSuccess(true);
        resultBean.setData(names);
        return resultBean;
    }

    /**
     * 请重新登录
     * @return
     */
    @GetMapping("relogin")
    public ResultBean<?> relogin(){
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);
        resultBean.setCode(-1);
        resultBean.setMessage("没有登录或者已过期，请重新登录。");
        return resultBean;
    }
}
